package com.lq.demo.demo.mode.timer;

import lombok.extern.slf4j.Slf4j;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @Auther: LQ
 * @Date: 2018/10/15 21:24
 * @Description:
 */
@Slf4j
public class WaterRebot extends TimerTask {

    //max=5
    private Integer bucketSize = 0;

    private Timer timer;

    public WaterRebot(Timer timer) {
        this.timer = timer;
    }

    @Override
    public void run() {
        //灌水 直到桶满
        if (bucketSize < 5) {
            log.info("add 1L  water  to  bucket");
            bucketSize++;
        } else {
            cancel();
            log.info("water is  full ");
            log.info(":" + timer.purge());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            timer.cancel();
        }
    }
}
